/**
 * 
 */
package pluginUtils;

// test

/**
 * Circle is defined with 
 * <ul>
 * <li> Its center (Complexe)</li>
 * <li> Radius (double)</li>
 * </ul>
 * @author decomite
 *
 */
public class Circle {
	
	protected Complexe center;
	protected double radius;

	
	public Circle(Pos3D p,double rayon){
		super();
		this.center=Complexe.createComplexeFromRI(p.x,p.z);
		this.radius=rayon;
	}
	
	public Circle(Point2D c,double rayon){
		super(); 
		this.center=Complexe.createComplexeFromRI(c.x,c.y);
		this.radius=rayon;
	}
	
	
	public Circle(Complexe center, double radius) {
		super();
		this.center = center;
		this.radius = radius;
	}
	
	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((center == null) ? 0 : center.hashCode());
		long temp;
		temp = Double.doubleToLongBits(radius);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Circle other = (Circle) obj;
		if (center == null) {
			if (other.center != null)
				return false;
		} else if (!center.equals(other.center))
			return false;
		if (Double.doubleToLongBits(radius) != Double
				.doubleToLongBits(other.radius))
			return false;
		return true;
	}

	public Complexe getCenter() {
		return center;
	}
	
	public Point2D getCenterAsPoint(){
		return new Point2D(center.getReal(),center.getIm()); 
	}
	
	public void setCenter(Complexe center) {
		this.center = center;
	}
	
	public double getRadius() {
		return radius;
	}
	
	public void setRadius(double radius) {
		this.radius = radius;
	} 
	
	public String toString(){
		return this.center+" "+this.radius;
		
	}
	public double getXcenter(){return this.center.getReal(); }
	public double getYcenter(){return this.center.getIm();}

	public void setXCenter(double xcenter) {
		// TODO Auto-generated method stub
		this.center.setReal(xcenter); 
	}
	public void setYCenter(double ycenter) {
		// TODO Auto-generated method stub
		this.center.setIm(ycenter); 
	}
	
	// Version Adhoc pour les chapeaux
	public Complexe[] intersection(Circle other){
		Complexe tableau[]=new Complexe[2];
		System.out.println("intersection"); 
		System.out.println(this); 
		System.out.println(other); 
		
		
		
		double distCentres=Complexe.distance(this.getCenter(), other.getCenter()); 
		double R0=this.getRadius(); 
		double R1=other.getRadius();
		
		System.out.println(distCentres+" "+R0+" "+R1); 
		// Cercle inclus dans l'autre
		if((distCentres+R0)<R1) return tableau; 
		if((distCentres+R1)<R0) return tableau;
		
		double x0=this.getXcenter(); 
		double y0=this.getYcenter(); 
		double x1=other.getXcenter(); 
		double y1=other.getYcenter(); 
		double N=(R1*R1-R0*R0-x1*x1+x0*x0-y1*y1+y0*y0)/(2*(y0-y1));
		double auxi=(x0-x1)/(y0-y1); 
		double A=auxi*auxi+1; 
		double B=2*y0*auxi-2*N*auxi-2*x0; 
		double C=x0*x0+y0*y0+N*N-R0*R0-2*y0*N;
		double deltaSquare=B*B-4*A*C; 
		if(deltaSquare<0) return tableau; 
		double delta=Math.sqrt(deltaSquare); 
		double racX0=(-B-delta)/(2*A); 
		double racY0=N-racX0*auxi; 
		tableau[0]=Complexe.createComplexeFromRI(racX0, racY0); 
		double racX1=(-B+delta)/(2*A); 
		double racY1=N-racX1*auxi; 
		tableau[1]=Complexe.createComplexeFromRI(racX1, racY1); 
		return tableau; 
		
	}

}
